package com.HeadlineSystem.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.HeadlineSystem.pojo.News;
import com.HeadlineSystem.pojo.Userinfo;
import com.HeadlineSystem.service.CategoryService;
import com.HeadlineSystem.service.NewsService;
import com.HeadlineSystem.service.impl.CategoryServiceImpl;
import com.HeadlineSystem.service.impl.NewsServiceImpl;
import com.HeadlineSystem.util.Constr;

public class NewsServlet extends HttpServlet {
	CategoryService categoryService = new CategoryServiceImpl();
	NewsService newService = new NewsServiceImpl();
	private static final long serialVersionUID = -9117568670831657252L;

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String action = request.getParameter("action");
		if (action == null || "fingIdexAll".equals(action)) {
			getIndexNewsInfo(request,response);
		}else if("addNewsInfo".equals(action)){
			addNewsInfo(request,response);
		}
	}

	private void addNewsInfo(HttpServletRequest request, HttpServletResponse response) throws IOException {
		PrintWriter out=response.getWriter();
		//验证是否登录成功
		
		Userinfo user =(Userinfo) request.getSession().getAttribute(Constr.user_info);
		if(user==null){
			out.write("<script type=\"text/javascript\">alert('用户没有登录，请先登录');location.href='jsp/login.jsp'</script>");
		}else{
			//��ȡ����t
			String title=request.getParameter("title");
			String categoryId=request.getParameter("categoryId");
			String titleImg=request.getParameter("titleImg");//获取标题图片
			String newsText=request.getParameter("newsText");
			
			News ne=new News();
			ne.setCategoryId(Integer.valueOf(categoryId));
			ne.setUserId(user.getUserId());
			ne.setTilteImg(titleImg);
			ne.setTitle(title);
			ne.setNewsText(newsText);
			
			boolean bool= newService.addNewsInfo(ne);
			if(bool){
				out.write("<script type=\"text/javascript\">alert('添加成功');location.href='jsp/profile_home.jsp'</script>");
			}else{
				out.write("<script type=\"text/javascript\">alert('添加失败');location.href='jsp/profile_home.jsp'</script>");
			}
		}			
	}

	private void getIndexNewsInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// ��ҳ������ѯ����
				String str = request.getParameter("indexPage");
				if (str == null) {
					str = "1";
				}
				Integer indexPage = Integer.valueOf(str);
				Integer pageSize = 20;

				// ��ҳ��ѯ����
				List<Map<String, Object>> list = newService.findPageAll(indexPage, pageSize);

				// ��ѯ�������
				List<Map<String, Object>> categoryList = categoryService.findCategoryAll();
				List<Map<String,Object>> hour24List = newService.findHour24();

				// ��װ���
				request.setAttribute("categoryList", categoryList);
				request.setAttribute("newList", list);
				request.setAttribute("hour24List", hour24List);
				
				// ҳ����ת
				request.getRequestDispatcher("jsp/home.jsp").forward(request, response);
	}
}
